import { json, error } from '@sveltejs/kit'; import { createAirtableServices } from '$lib/airtable'; import type { RequestHandler } from './$types'; const apiKey = process.env.VITE_AIRTABLE_API_KEY; const baseId = process.env.VITE_AIRTABLE_BASE_ID; if (!apiKey || !baseId) { throw new Error('Airtable API key and base ID must be configured'); } const airtable = createAirtableServices(apiKey, baseId); // GET /api/watering-logs/[id] - Get a specific watering log export const GET: RequestHandler = async ({ params }) => { try { const { id } = params; const record = await airtable.wateringLogs.getRecord(id); const log = { id: record.id, ...record.fields, wateredAt: record.fields.wateredAt ? new Date(record.fields.wateredAt) : new Date() }; return json(log); } catch (err) { console.error('Error fetching watering log:', err); throw error(404, 'Watering log not found'); } }; // PUT /api/watering-logs/[id] - Update a watering log export const PUT: RequestHandler = async ({ params, request }) => { try { const { id } = params; const logData = await request.json(); const fields = { ...logData, wateredAt: logData.wateredAt?.toISOString() }; const record = await airtable.wateringLogs.updateRecord(id, fields); const log = { id: record.id, ...record.fields, wateredAt: record.fields.wateredAt ? new Date(record.fields.wateredAt) : new Date() }; return json(log); } catch (err) { console.error('Error updating watering log:', err); throw error(500, 'Failed to update watering log'); } }; // DELETE /api/watering-logs/[id] - Delete a watering log export const DELETE: RequestHandler = async ({ params }) => { try { const { id } = params; await airtable.wateringLogs.deleteRecord(id); return json({ success: true }); } catch (err) { console.error('Error deleting watering log:', err); throw error(500, 'Failed to delete watering log'); } };